/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ClerkSearchUserPanel.java
 *
 * Created on 8-Apr-2010, 5:06:00 PM
 */
package ticketmaster.presentation.clerk;

import java.util.*;
import javax.swing.*;
import ticketmaster.dao.*;
import ticketmaster.businesslogic.*;
import ticketmaster.businessobject.LoginUser;
import ticketmaster.presentation.customer.purchase.SearchView;

/**
 *
 * @author Xiaolong Xue
 */
public class ClerkSearchUserPanel extends ClerkBasePanel
{

    /** Creates new form ClerkSearchUserPanel */
    public ClerkSearchUserPanel()
    {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        btnSearch = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ticketmaster.TicketMasterApp.class).getContext().getResourceMap(ClerkSearchUserPanel.class);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setName("Form"); // NOI18N
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 240, -1, -1));

        jPanel2.setBackground(resourceMap.getColor("jPanel2.background")); // NOI18N
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("jPanel2.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("jPanel2.border.titleFont"))); // NOI18N
        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        jPanel2.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, -1, 50));

        txtUsername.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        txtUsername.setText(resourceMap.getString("txtUsername.text")); // NOI18N
        txtUsername.setName("txtUsername"); // NOI18N
        jPanel2.add(txtUsername, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 40, 300, 50));

        btnSearch.setFont(resourceMap.getFont("btnSearch.font")); // NOI18N
        btnSearch.setText(resourceMap.getString("btnSearch.text")); // NOI18N
        btnSearch.setName("btnSearch"); // NOI18N
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });
        jPanel2.add(btnSearch, new org.netbeans.lib.awtextra.AbsoluteConstraints(470, 40, 170, 50));

        add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 670, 150));
    }// </editor-fold>//GEN-END:initComponents

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        // TODO add your handling code here:
        searchUser();
    }//GEN-LAST:event_btnSearchActionPerformed

    private void searchUser()
    {
        ticketmaster.dataobject.User userDO = new ticketmaster.dataobject.User();
        userDO.setUsername(txtUsername.getText());
        Collection<ticketmaster.dataobject.User> users = DAOFactory.getInstance().getUserDAO().getUsersDO(userDO);
        if (users.size() == 0)
        {
            JOptionPane.showMessageDialog(null, "User name does not exist", "Search user",
                                          JOptionPane.INFORMATION_MESSAGE);
        }
        else
        {
            Iterator<ticketmaster.dataobject.User> userDOIter = users.iterator();
            if (userDOIter.hasNext())
            {
                ticketmaster.businessobject.User userBO = UserManager.getUserBO(userDOIter.next());
//                setUser(userBO);
                LoginUser.setUser(userBO);
                JOptionPane.showMessageDialog(null, "User search success", "Search user",
                                              JOptionPane.INFORMATION_MESSAGE);

                SearchView sv = new SearchView();
                sv.setPnlParent(this.getPnlParent());
                this.getPnlParent().removeAll();
                this.getPnlParent().add(sv);
                this.getPnlParent().validate();
                this.getPnlParent().repaint();
            }
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSearch;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables
}
